package com.xbongbong.pro.message;

import com.alibaba.dingtalk.openapi.newmessage.entity.OA;
import com.alibaba.fastjson.JSON;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.URLEncodeUtils;
import com.xbongbong.paas.toolbox.util.UrlUtil;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.message.enums.MessageWebPcUrlEnum;
import com.xbongbong.pro.message.pojo.PushRefTypePojo;
import com.xbongbong.saas.enums.AiAssistantDingtalkPageUrlEnum;
import com.xbongbong.saas.enums.DingtalkPageUrlEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/13 10:40
 */
public class SendMessageUtil {

    /**
     * 解析手机端列表url
     * @param corpid
     * @param saasMark
     * @param businessType
     * @param name
     * @param title
     * @param content
     * @param formList
     * @param options
     * @param messageUrl
     * @param pcMessageUrl
     * @return
     */
    public static OA formatOaMessageDingtalkUrl(String corpid, Integer saasMark, Integer businessType, String name, String title, String content, List<OA.Body.Form> formList, PushRefTypePojo.Options options, String messageUrl, String pcMessageUrl) {

        OA oaMessage = new OA();
        OA.Body body = new OA.Body();
        body.setForm(formList);
        body.setTitle(title);
        body.setContent(content);
        body.setAuthor(I18nMessageUtil.getMessage(I18nStringConstant.XBB_CRM));
        oaMessage.setBody(body);
        //OA消息封装
        if (Objects.equals(SaasMarkEnum.PAAS.getCode(), saasMark)) {
            messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.PAAS_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getDataIds(), name);

        } else {
            XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
            switch (xbbRefTypeEnum) {
                case CUSTOMER_MANAGEMENT:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.CUSTOMER_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds(), options.getFormId());
                    break;
                case CONTACT:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.CONTACT_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case SALES_OPPORTUNITY:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.OPPORTUNITY_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case CONTRACT:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.CONTRACT_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds(), options.getFormId());
                    break;
//            case WORK_REPORT_PUSH:
//                messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.WORK_REPORT_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId());
//                break;
                case REFUND:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.REFUND_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case QUOTATION:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.QUOTATION_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case SUPPLIER:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.SUPPLIER_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case SUPPLIER_CONTACT:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.SUPPLIER_CONTACT_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case PURCHASE:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.PURCHASE_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case RETURNED_PURCHASE:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.PURCHASE_RETURN_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case INSTOCK:
                case PURCHASE_INSTOCK:
                case OTHER_INSTOCK:
                case REFUND_INSTOCK:
                case RETURNED_MATERIEL_INSTOCK:
                case PRODUCTION_INSTOCK:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.NEW_INSTOCK_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getBusinessType(), options.getBusinessType(), options.getDataIds());
                    break;
                case OUTSTOCK:
                case CONTRACT_OUTSTOCK:
                case OTHER_OUTSTOCK:
                case RETURNED_PURCHASE_OUTSTOCK:
                case WORK_ORDER_OUTSTOCK:
                case PRODUCTION_MATERIEL_OUTSTOCK:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.NEW_OUTSTOCK_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getBusinessType(), options.getBusinessType(), options.getDataIds());
                    break;
                case ASSEMBLE:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.ASSEMBLE_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case TRANSFER:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.TRANSFER_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case INVENTORY:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.IINVENTORY_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case BOM_BILL:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.BOMBILL_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case PRODUCTION_ORDER:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.PRODUCTION_ORDER_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case PAYMENT:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.PAYMENT_PLAN_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case INVOICE:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.INVOICE_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case PURCHASE_INVOICE:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.PURCHASE_INVOICE_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case PAY_PLAN:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.PAY_PLAN_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds());
                    break;
                case WORK_ORDER:
                    messageUrl += String.format(DingtalkPageUrlEnum.WORK_ORDER_LIST.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case PRODUCT:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.PRODUCT_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getDataIds());
                    break;
                case MARKET_ACTIVITY:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.MARKET_ACTIVITY_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds(), options.getFormId());
                    break;
                case CLUE:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.CLUE_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getDataIds(), options.getFormId());
                    break;

                case WORK_ORDER_V2:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.WORK_ORDER_V2_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getSaasMark(), options.getBusinessType(), options.getSubBusinessType(), options.getDataIds(), options.getFormId());
                    break;
                case RECEIPT_ORDER:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.RECEIPT_ORDER_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getSaasMark(), options.getBusinessType(), options.getSubBusinessType(), options.getDataIds(), options.getFormId());
                    break;
                case SERVICE_PROJECT:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.SERVICE_PROJECT_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId(), options.getSaasMark(), options.getBusinessType(), options.getSubBusinessType(), options.getDataIds(), options.getFormId());
                    break;
                case UNKNOWN:
                    break;
                default:
                    messageUrl += String.format(AiAssistantDingtalkPageUrlEnum.INDEX.getUrl(), corpid);
                    break;
            }
        }
        pcMessageUrl += URLEncodeUtils.encodeURL(messageUrl) + "&pc_slide=true";
        oaMessage.setMessageUrl(messageUrl);
        oaMessage.setPcMessageUrl(pcMessageUrl);

        return oaMessage;
    }

    /**
     * 解析pc端的列表url
     * @param businessType
     * @param saasMark
     * @param options
     * @return
     * @throws XbbException
     */
    public static String formatRefTypeListUrl(Integer businessType, Integer saasMark, PushRefTypePojo.Options options) throws XbbException {
        PushRefTypePojo pushRefTypePojo = new PushRefTypePojo();
        Map<String, Object> optionMap;
        if (Objects.equals(SaasMarkEnum.PAAS.getCode(), saasMark)) {
            PushRefTypePojo.CommonListOptions paasListOptions = new PushRefTypePojo.CommonListOptions();
            BeanUtil.copyProperties(options, paasListOptions);
            optionMap = BeanUtil.convertBean2Map(paasListOptions);
            pushRefTypePojo.setType(MessageWebPcUrlEnum.PAAS_LIST.getType());
            // 添加选择的formId
            optionMap.put("defaultFormId", options.getFormId());
            pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.PAAS_LIST.getUrl(), optionMap));
            pushRefTypePojo.setOptions(paasListOptions);
            return JSON.toJSONString(pushRefTypePojo);
        }
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
        switch (xbbRefTypeEnum) {
            // 客户
            case CUSTOMER_MANAGEMENT:
                PushRefTypePojo.CommonListOptions customerListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, customerListOptions);
                optionMap = BeanUtil.convertBean2Map(customerListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.CUSTOMER_LIST.getType());
                // 添加选择的formId
                optionMap.put("defaultFormId", options.getFormId());
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.CUSTOMER_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(customerListOptions);
                break;
            // 联系人
            case CONTACT:
                PushRefTypePojo.CommonListOptions contactListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, contactListOptions);
                optionMap = BeanUtil.convertBean2Map(contactListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.CONTACT_LIST.getType());

                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.CONTACT_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(contactListOptions);
                break;
            // 机会
            case SALES_OPPORTUNITY:
                PushRefTypePojo.CommonListOptions opportunityListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, opportunityListOptions);
                optionMap = BeanUtil.convertBean2Map(opportunityListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.OPPORTUNITY_LIST.getType());

                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.OPPORTUNITY_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(opportunityListOptions);
                break;
            case CUSTOMER_COMMUNICATE:
            case SUPPLIER_COMMUNICATE:
                pushRefTypePojo.setType(MessageWebPcUrlEnum.COMMUNICATE_DETAIL.getType());
                PushRefTypePojo.SpecialDetailOptions specialDetailOptions = new PushRefTypePojo.SpecialDetailOptions();
                BeanUtil.copyProperties(options, specialDetailOptions);
                specialDetailOptions.setDataIds(options.getDataIds());
                optionMap = BeanUtil.convertBean2Map(specialDetailOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.COMMUNICATE_DETAIL.getUrl(), optionMap));
                break;
            // 合同
            case CONTRACT:
                PushRefTypePojo.CommonListOptions contractListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, contractListOptions);
                optionMap = BeanUtil.convertBean2Map(contractListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.CONTRACT_LIST.getType());
                // 添加选择的formId
                optionMap.put("defaultFormId", options.getFormId());
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.CONTRACT_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(contractListOptions);
                break;
            // 退货退款
            case REFUND:
                PushRefTypePojo.CommonListOptions refundListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, refundListOptions);
                optionMap = BeanUtil.convertBean2Map(refundListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.REFUND_LIST.getType());

                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.REFUND_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(refundListOptions);
                break;
            // 工作报告
//            case WORK_REPORT:
//                PushRefTypePojo.CommonListOptions workReportListOptions = new PushRefTypePojo.CommonListOptions();
//                BeanUtil.copyProperties(options, workReportListOptions);
//                optionMap = BeanUtil.convertBean2Map(workReportListOptions);
//                pushRefTypePojo.setType(MessageWebPcUrlEnum.WORK_REPORT_LIST.getType());
//                pushRefTypePojo.setOptions(workReportListOptions);
//                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.WORK_REPORT_LIST.getUrl(), optionMap));
//                break;
            // 报价单
            case QUOTATION:
                PushRefTypePojo.CommonListOptions quotationListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, quotationListOptions);
                optionMap = BeanUtil.convertBean2Map(quotationListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.QUOTATION_LIST.getType());
                pushRefTypePojo.setOptions(quotationListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.QUOTATION_LIST.getUrl(), optionMap));
                break;

            // 供应商
            case SUPPLIER:
                PushRefTypePojo.CommonListOptions supplierListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, supplierListOptions);
                optionMap = BeanUtil.convertBean2Map(supplierListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.SUPPLIER_LIST.getType());
                pushRefTypePojo.setOptions(supplierListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.SUPPLIER_LIST.getUrl(), optionMap));
                break;
            // 供应商联系人
            case SUPPLIER_CONTACT:
                PushRefTypePojo.CommonListOptions supplierContactListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, supplierContactListOptions);
                optionMap = BeanUtil.convertBean2Map(supplierContactListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.CONTACT_LIST.getType());
                pushRefTypePojo.setOptions(supplierContactListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.CONTACT_LIST.getUrl(), optionMap));
                break;
            // 采购合同
            case PURCHASE:
                PushRefTypePojo.CommonListOptions purchaseContractListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, purchaseContractListOptions);
                optionMap = BeanUtil.convertBean2Map(purchaseContractListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.PURCHASE_CONTRACT_LIST.getType());
                pushRefTypePojo.setOptions(purchaseContractListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.PURCHASE_CONTRACT_LIST.getUrl(), optionMap));
                break;
            // 采购退货单
            case RETURNED_PURCHASE:
                PushRefTypePojo.CommonListOptions purchaseReturnListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, purchaseReturnListOptions);
                optionMap = BeanUtil.convertBean2Map(purchaseReturnListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.PURCHASE_RETURN_LIST.getType());
                pushRefTypePojo.setOptions(purchaseReturnListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.PURCHASE_RETURN_LIST.getUrl(), optionMap));
                break;

            // 入库单
            case INSTOCK:
            case PURCHASE_INSTOCK:
            case OTHER_INSTOCK:
            case REFUND_INSTOCK:
            case RETURNED_MATERIEL_INSTOCK:
            case PRODUCTION_INSTOCK:
                PushRefTypePojo.CommonListOptions inWarehouseListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, inWarehouseListOptions);
                optionMap = BeanUtil.convertBean2Map(inWarehouseListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.IN_WAREHOUSE_LIST.getType());
                pushRefTypePojo.setOptions(inWarehouseListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.IN_WAREHOUSE_LIST.getUrl(), optionMap));
                break;
            // 出库单
            case OUTSTOCK:
            case CONTRACT_OUTSTOCK:
            case OTHER_OUTSTOCK:
            case RETURNED_PURCHASE_OUTSTOCK:
            case WORK_ORDER_OUTSTOCK:
            case PRODUCTION_MATERIEL_OUTSTOCK:
                PushRefTypePojo.CommonListOptions outWarehouseListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, outWarehouseListOptions);
                optionMap = BeanUtil.convertBean2Map(outWarehouseListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.OUT_WAREHOUSE_LIST.getType());
                pushRefTypePojo.setOptions(outWarehouseListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.OUT_WAREHOUSE_LIST.getUrl(), optionMap));
                break;

            // 出入库单
            case ASSEMBLE:
                PushRefTypePojo.CommonListOptions assembleListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, assembleListOptions);
                optionMap = BeanUtil.convertBean2Map(assembleListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.ASSEMBLE_LIST.getType());
                pushRefTypePojo.setOptions(assembleListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.ASSEMBLE_LIST.getUrl(), optionMap));
                break;
            // 调拔单
            case TRANSFER:
                PushRefTypePojo.CommonListOptions transferListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, transferListOptions);
                optionMap = BeanUtil.convertBean2Map(transferListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.ALLOT_LIST.getType());
                pushRefTypePojo.setOptions(transferListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.ALLOT_LIST.getUrl(), optionMap));
                break;
            // 盘点单
            case INVENTORY:
                PushRefTypePojo.CommonListOptions inventoryListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, inventoryListOptions);
                optionMap = BeanUtil.convertBean2Map(inventoryListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.INVENTORY_LIST.getType());
                pushRefTypePojo.setOptions(inventoryListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.INVENTORY_LIST.getUrl(), optionMap));
                break;

            // bom
            case BOM_BILL:
                PushRefTypePojo.CommonListOptions bomListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, bomListOptions);
                optionMap = BeanUtil.convertBean2Map(bomListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.BOM_LIST.getType());
                pushRefTypePojo.setOptions(bomListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.BOM_LIST.getUrl(), optionMap));
                break;
            // 生产单
            case PRODUCTION_ORDER:
                PushRefTypePojo.CommonListOptions productionOrderListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, productionOrderListOptions);
                optionMap = BeanUtil.convertBean2Map(productionOrderListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.PRODUCTION_ORDER_LIST.getType());
                pushRefTypePojo.setOptions(productionOrderListOptions);
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.PRODUCTION_ORDER_LIST.getUrl(), optionMap));
                break;

            // 回款计划
            case PAYMENT:
                PushRefTypePojo.CommonListOptions collectionOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, collectionOptions);
                optionMap = BeanUtil.convertBean2Map(collectionOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.COLLECTION_LIST.getType());
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.COLLECTION_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(collectionOptions);
                break;
            // 付款计划
            case PAY_PLAN:
                PushRefTypePojo.CommonListOptions paymentOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, paymentOptions);
                optionMap = BeanUtil.convertBean2Map(paymentOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.PAYMENT_LIST.getType());
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.PAYMENT_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(paymentOptions);
                break;
            // 发票
            case INVOICE:
                PushRefTypePojo.CommonListOptions invoiceOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, invoiceOptions);
                optionMap = BeanUtil.convertBean2Map(invoiceOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.INVOICE_LIST.getType());
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.INVOICE_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(invoiceOptions);
                break;
            case PURCHASE_INVOICE:
                PushRefTypePojo.CommonListOptions purchaseInvoiceOptions = new PushRefTypePojo.CommonListOptions();
                optionMap = BeanUtil.convertBean2Map(purchaseInvoiceOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.PURCHASE_INVOICE_LIST.getType());
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.PURCHASE_INVOICE_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(purchaseInvoiceOptions);
                break;
            // 产品
            case PRODUCT:
                PushRefTypePojo.CommonListOptions productOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, productOptions);
                optionMap = BeanUtil.convertBean2Map(productOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.PRODUCT_MANEGEMENT_LIST.getType());
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.PRODUCT_MANEGEMENT_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(productOptions);
                break;
            case MARKET_MANAGEMENT:
                PushRefTypePojo.CommonListOptions marketActivityListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, marketActivityListOptions);
                optionMap = BeanUtil.convertBean2Map(marketActivityListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.MARKET_ACTIVITY_LIST.getType());
                // 添加选择的formId
                optionMap.put("defaultFormId", options.getFormId());
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.MARKET_ACTIVITY_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(marketActivityListOptions);
                break;
            case CLUE:
                PushRefTypePojo.CommonListOptions clueListOptions = new PushRefTypePojo.CommonListOptions();
                BeanUtil.copyProperties(options, clueListOptions);
                optionMap = BeanUtil.convertBean2Map(clueListOptions);
                pushRefTypePojo.setType(MessageWebPcUrlEnum.CLUE_LIST.getType());
                // 添加选择的formId
                optionMap.put("defaultFormId", options.getFormId());
                pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.CLUE_LIST.getUrl(), optionMap));
                pushRefTypePojo.setOptions(clueListOptions);
                break;
            case UNKNOWN:
                break;
            default:
        }
        return JSON.toJSONString(pushRefTypePojo);
    }

    /**
     * 手机端详情的链接和钉钉的链接
     * @param corpid
     * @param saasMark
     * @param businessType
     * @param name
     * @param title
     * @param content
     * @param formList
     * @param options
     * @param messageUrl 手机端详情的url
     * @param pcMessageUrl 钉钉的详情的url
     * @return
     */
    public static OA detailFormatOaMessage(String corpid, Integer saasMark, Integer businessType, String name, String title, String content, List<OA.Body.Form> formList, PushRefTypePojo.Options options,  String messageUrl, String pcMessageUrl) {
//        String messageUrl = proBaseConfig.getPushFrontUrl() + "index.html?corpid=" + corpid + "&appid=" + Env.DING_XBB_APP_ID + "&dd_nav_bgcolor=ffff943e#";
//        String pcMessageUrl = "dingtalk://dingtalkclient/page/link?url=";
        OA oaMessage = new OA();
        OA.Body body = new OA.Body();
        body.setTitle(title);
        body.setForm(formList);
        body.setContent(content);
        body.setAuthor(I18nMessageUtil.getMessage(I18nStringConstant.XBB_CRM));
        oaMessage.setBody(body);
        if (Objects.equals(SaasMarkEnum.PAAS.getCode(), saasMark)) {
            messageUrl += String.format(DingtalkPageUrlEnum.PAAS_FORM_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId(), name);

        } else {
            //OA消息封装
            XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);
            switch (xbbRefTypeEnum) {
                case CUSTOMER_MANAGEMENT:
                    messageUrl += String.format(DingtalkPageUrlEnum.CUSTOMER_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case CONTACT:
                    messageUrl += String.format(DingtalkPageUrlEnum.CONTACT_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;

                case SALES_OPPORTUNITY:
                    messageUrl += String.format(DingtalkPageUrlEnum.OPPORTUNITY_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case COMMUNICATE_PLAN:
                    messageUrl += String.format(DingtalkPageUrlEnum.COMMUNICATE_PLAN_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;

                case CUSTOMER_COMMUNICATE:
                    messageUrl += String.format(DingtalkPageUrlEnum.COMMUNICATE_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());

                    break;

                case CONTRACT:
                    messageUrl += String.format(DingtalkPageUrlEnum.CONTRACT_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;

                case PAYMENT:
                    messageUrl += String.format(DingtalkPageUrlEnum.PAYMENT_PLAN_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case PAYMENT_SHEET:
                    messageUrl += String.format(DingtalkPageUrlEnum.PAYMENT_SHEET_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case PAY_SHEET:
                    messageUrl += String.format(DingtalkPageUrlEnum.PAY_SHEET_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;

                case PAY_PLAN:
                    messageUrl += String.format(DingtalkPageUrlEnum.PAY_PLAN_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;

                case INVOICE:
                    messageUrl += String.format(DingtalkPageUrlEnum.INVOICE_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case PURCHASE_INVOICE:
                    messageUrl += String.format(DingtalkPageUrlEnum.PURCHASE_INVOICE_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
//                case WORK_REPORT:
//                    messageUrl += String.format(DingtalkPageUrlEnum.WORK_REPORT_LIST.getUrl(), options.getAppId(), options.getMenuId(), options.getFormId());
//                    break;
                case PURCHASE:
                    messageUrl += String.format(DingtalkPageUrlEnum.PURCHASE_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case INSTOCK:
                case PURCHASE_INSTOCK:
                case OTHER_INSTOCK:
                case REFUND_INSTOCK:
                case RETURNED_MATERIEL_INSTOCK:
                case PRODUCTION_INSTOCK:
                    messageUrl += String.format(DingtalkPageUrlEnum.INSTOCK_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case OUTSTOCK:
                case CONTRACT_OUTSTOCK:
                case OTHER_OUTSTOCK:
                case RETURNED_PURCHASE_OUTSTOCK:
                case WORK_ORDER_OUTSTOCK:
                case PRODUCTION_MATERIEL_OUTSTOCK:
                    messageUrl += String.format(DingtalkPageUrlEnum.OUTSTOCK_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case SUPPLIER:
                    messageUrl += String.format(DingtalkPageUrlEnum.SUPPLIER_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case SUPPLIER_COMMUNICATE:
                    messageUrl += String.format(DingtalkPageUrlEnum.JXC_COMMUNICATE_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case BOM_BILL:
                    messageUrl += String.format(DingtalkPageUrlEnum.BOMBILL_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case PRODUCTION_ORDER:
                    messageUrl += String.format(DingtalkPageUrlEnum.PRODUCTION_ORDER_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case REFUND:
                    messageUrl += String.format(DingtalkPageUrlEnum.REFUND_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case WORK_ORDER:
                    messageUrl += String.format(DingtalkPageUrlEnum.WORK_ORDER_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;

                case QUOTATION:
                    messageUrl += String.format(DingtalkPageUrlEnum.QUOTATION_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                    break;
                case CLUE:
                    messageUrl += String.format(DingtalkPageUrlEnum.CLUE_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId());
                case WORK_ORDER_V2:
                    messageUrl += String.format(DingtalkPageUrlEnum.WORK_ORDER_V2_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId(),options.getBusinessType(), options.getSaasMark(), options.getSubBusinessType());
                    break;
                case RECEIPT_ORDER:
                    messageUrl += String.format(DingtalkPageUrlEnum.RECEIPT_ORDER_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId(),options.getBusinessType(), options.getSaasMark(), options.getSubBusinessType());
                    break;
                case SERVICE_PROJECT:
                    messageUrl += String.format(DingtalkPageUrlEnum.SERVICE_PROJECT_DETAIL.getUrl(), options.getDataId(), options.getAppId(), options.getMenuId(), options.getFormId(),options.getBusinessType(), options.getSaasMark(), options.getSubBusinessType());
                    break;
                case UNKNOWN:
                    break;
                default:
                    messageUrl += String.format(DingtalkPageUrlEnum.INDEX.getUrl(), corpid);
                    break;
            }
        }
        // pc_slide=true 表示从钉钉的侧边栏滑出打开，不可去掉
        pcMessageUrl += URLEncodeUtils.encodeURL(messageUrl) + "&pc_slide=true";
        oaMessage.setMessageUrl(messageUrl);
        oaMessage.setPcMessageUrl(pcMessageUrl);

        return oaMessage;
    }


    public static PushRefTypePojo.Options getDetailOptions(Long appId, Long menuId, Long formId, Long dataId, Integer saasMark, Integer businessType, Integer subBusinessType) {
        List<Integer> list = Arrays.asList(XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode(), XbbRefTypeEnum.SUPPLIER_COMMUNICATE.getCode());
        if (list.contains(businessType)) {
            return new PushRefTypePojo.Options(appId, menuId, formId, dataId, saasMark, businessType, subBusinessType);
        } else {
            return new PushRefTypePojo.Options(appId, menuId, formId, dataId, saasMark, businessType);
        }
    }
    public static String getDetail(PushRefTypePojo.Options options) throws XbbException {
        PushRefTypePojo pushRefTypePojo = new PushRefTypePojo();
        List<Integer> list = Arrays.asList(XbbRefTypeEnum.CUSTOMER_COMMUNICATE.getCode(), XbbRefTypeEnum.SUPPLIER_COMMUNICATE.getCode());
        if (list.contains(options.getBusinessType())) {
            pushRefTypePojo.setType(MessageWebPcUrlEnum.COMMUNICATE_DETAIL.getType());
            PushRefTypePojo.SpecialDetailOptions specialDetailOptions = new PushRefTypePojo.SpecialDetailOptions();
            BeanUtil.copyProperties(options, specialDetailOptions);
            specialDetailOptions.setDataIds(Arrays.asList(options.getDataId()));
            Map<String, Object> optionMap = BeanUtil.convertBean2Map(specialDetailOptions);
            pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.COMMUNICATE_DETAIL.getUrl(), optionMap));
        } else {
            pushRefTypePojo.setType(MessageWebPcUrlEnum.COMMON_DETAIL.getType());
            PushRefTypePojo.CommonDetailOptions commonDetailOptions = new PushRefTypePojo.CommonDetailOptions();
            BeanUtil.copyProperties(options, commonDetailOptions);
            Map<String, Object> optionMap = BeanUtil.convertBean2Map(commonDetailOptions);
            pushRefTypePojo.setPcUrl(UrlUtil.formatPcUrl(MessageWebPcUrlEnum.COMMON_DETAIL.getUrl(), optionMap));
            pushRefTypePojo.setOptions(commonDetailOptions);
        }
        return JSON.toJSONString(pushRefTypePojo);
    }
}
